import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import './assets/gloable.css'

axios.defaults.baseURL = process.env.VUE_APP_SERVER

Vue.prototype.axios = axios

Vue.use(ElementUI);

Vue.config.productionTip = false



console.log("环境:" , process.env.NODE_ENV)

/*拦截器*/
axios.interceptors.request.use(
    config => {
        const tokenStr = window.localStorage.getItem('cecAdminToken')
        //console.log(tokenStr)
        if (tokenStr) {
            config.headers['Authorization'] = tokenStr
        }
        console.log("请求参数:", config);
        return config;
    }, error => {
        return Promise.reject(error);
    }
)

router.beforeEach((to, from, next) => {
    const tokenStr = window.localStorage.getItem('cecAdminToken');
    if (to.path == "/") {
        next();
    } else if (!tokenStr) {
        ElementUI.Message({
            message: '请先登录!',
            type: 'error'
        });
        next({path: "/"});
    } else {
        next();
    }
});


axios.interceptors.response.use(response => {
        if (response.data.state == 40900) {
            if (window.localStorage.getItem("cecAdminToken") != null) {
                console.log("即将跳转页面")
                router.push("/");
                window.localStorage.removeItem("cecAdminToken");
                ElementUI.Message({
                    message: '身份已失效,请重新登录',
                    type: 'error'
                });
            }
        } else if (response.data.state == 40901 || response.data.state == 40902 || response.data.state == 50100) {
            if (window.localStorage.getItem("cecAdminToken") != null) {
                router.push("/");
                ElementUI.Message({
                    message: response.data.message,
                    type: 'error'
                });
                window.localStorage.removeItem("cecAdminToken");
                //跳转
            }
        } else {
            console.log("返回结果:", response);
            return response
        }
    }, error => {
        console.log("返回错误:", error);
        return Promise.reject(error);
    }
)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
